package com.io;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by zhuangqi on 17-3-26.
 */
public class OutputStreamTest {
    public static void main(String[] args) {
        read();
    }
    private void write(){
         try {
            String data = "hello,123,jhoncy";
            OutputStream output = new FileOutputStream("hello.text");
            byte[] bytes = data.getBytes(Charset.forName("UTF-8"));
            output.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void read() {
        InputStream input = null;
        try {
            input = new FileInputStream("hello.text");
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead=input.read(buff))!=-1){
                output.write(buff,0,bytesRead);
            }
            String data = output.toString("UTF-8");
            System.out.println(data);
            }
            catch (IOException e) {
                e.printStackTrace();
            }finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
